package com.fudan.backend.service.impl;

import com.fudan.backend.dao.ActivityDao;
import com.fudan.backend.dao.CartDao;
import com.fudan.backend.dao.CommodityDao;
import com.fudan.backend.dao.StoreDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Activity;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.service.CommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    CommodityDao commodityDao;

    @Autowired
    CartDao cartDao;

    @Autowired
    ActivityDao activityDao;

    @Autowired
    StoreDao storeDao;

    @Override
    public Commodity getCommodityByCommodityId(String commodityId) {
        Commodity commodity = commodityDao.selectCommodityByCommodityId(commodityId);
        if(commodity == null){
            throw new BaseException(RetCode.COMMODITY_NOT_FOUND.getCode(),RetCode.COMMODITY_NOT_FOUND.getMessage());
        }
        return commodity;
    }

    @Override
    public PageInfo<Commodity> getAllCommodityByStoreId(int current, int pageSize, String storeId) {
        PageHelper.startPage(current, pageSize);
        List<Commodity> commodities = commodityDao.selectAllCommodityByStoreId(storeId);
        return new PageInfo<Commodity>(commodities, 5);
    }

    @Override
    public PageInfo<Commodity> getAllCommodity(int current, int pageSize) {
        PageHelper.startPage(current, pageSize);
        List<Commodity> commodities = commodityDao.selectAllCommodity();
        return new PageInfo<Commodity>(commodities, 5);
    }

    @Override
    public List<Commodity> getAllCommodityByActivityId(String activityId) {
        List<String> storeIdList = storeDao.selectStoreIdByActivityId(activityId);
        Activity activity = activityDao.selectActivityByActivityId(activityId);
        if(activity == null){
            throw new BaseException(RetCode.ACTIVITY_NOT_FOUND);
        }
        List<String> categoryIdList = activity.getCategoryIdList();

        List<Commodity> commodityList = new ArrayList<>();

        for (String storeId : storeIdList) {
            for (String categoryId : categoryIdList) {
                List<Commodity> commodities = commodityDao.selectAllCommodityByStoreIdAndCategoryId(storeId, categoryId);
                commodityList.addAll(commodities);
            }
        }

        return commodityList;
    }

    @Override
    @Transactional
    public RetCode setCommodityStatus(String commodityId, Integer status) {
        Integer ret1 = commodityDao.setCommodityStatus(commodityId, status);
        Integer ret2 = cartDao.setStatusByCommodityId(commodityId, status);
        if(ret1.equals(0) && ret2.equals(0)){
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        return RetCode.COMMODITY_SUCCESS;
    }

}
